unit JudoMod;

interface

uses
  SysUtils, Classes, DB, DBClient, RpRave, RpDefine, RpCon, RpConDS,
  Provider;

type
  TCatPeso = (catBambini, catUnder19M, catUnder19F, catRagazziM, catRagazziF,
    catEsAM, catEsBM, catEsAF, catEsBF, catCadettiM, catCadettiF, catJuniorM,
    catJuniorF, catSeniorM, catSeniorF, catMasterM1, catMasterF1);

  TmodJudo = class(TDataModule)
    cdsTabellone32: TClientDataSet;
    cdsTabellone16: TClientDataSet;
    cdsTabellone8: TClientDataSet;
    cdsTabellone4: TClientDataSet;
    cdsTabellone32Atleta1: TStringField;
    cdsTabellone32Atleta2: TStringField;
    cdsTabellone32Atleta3: TStringField;
    cdsTabellone32Atleta4: TStringField;
    cdsTabellone32Atleta5: TStringField;
    cdsTabellone32Atleta6: TStringField;
    cdsTabellone32Atleta7: TStringField;
    cdsTabellone32Atleta8: TStringField;
    cdsTabellone32Atleta9: TStringField;
    cdsTabellone32Atleta10: TStringField;
    cdsTabellone32Atleta11: TStringField;
    cdsTabellone32Atleta12: TStringField;
    cdsTabellone32Atleta13: TStringField;
    cdsTabellone32Atleta14: TStringField;
    cdsTabellone32Atleta15: TStringField;
    cdsTabellone32Atleta16: TStringField;
    cdsTabellone32Atleta17: TStringField;
    cdsTabellone32Atleta18: TStringField;
    cdsTabellone32Atleta19: TStringField;
    cdsTabellone32Atleta20: TStringField;
    cdsTabellone32Atleta21: TStringField;
    cdsTabellone32Atleta22: TStringField;
    cdsTabellone32Atleta23: TStringField;
    cdsTabellone32Atleta24: TStringField;
    cdsTabellone32Atleta25: TStringField;
    cdsTabellone32Atleta26: TStringField;
    cdsTabellone32Atleta27: TStringField;
    cdsTabellone32Atleta28: TStringField;
    cdsTabellone32Atleta29: TStringField;
    cdsTabellone32Atleta30: TStringField;
    cdsTabellone32Atleta31: TStringField;
    cdsTabellone32Atleta32: TStringField;
    cdsPalestreApp: TClientDataSet;
    cdsPalestreAppCodice: TIntegerField;
    cdsPalestreAppNome: TStringField;
    cdsTabellone16Atleta1: TStringField;
    cdsTabellone16Atleta2: TStringField;
    cdsTabellone16Atleta3: TStringField;
    cdsTabellone16Atleta4: TStringField;
    cdsTabellone16Atleta5: TStringField;
    cdsTabellone16Atleta6: TStringField;
    cdsTabellone16Atleta7: TStringField;
    cdsTabellone16Atleta8: TStringField;
    cdsTabellone16Atleta9: TStringField;
    cdsTabellone16Atleta10: TStringField;
    cdsTabellone16Atleta11: TStringField;
    cdsTabellone16Atleta12: TStringField;
    cdsTabellone16Atleta13: TStringField;
    cdsTabellone16Atleta14: TStringField;
    cdsTabellone16Atleta15: TStringField;
    cdsTabellone16Atleta16: TStringField;
    cdsTabellone8Atleta1: TStringField;
    cdsTabellone8Atleta2: TStringField;
    cdsTabellone8Atleta3: TStringField;
    cdsTabellone8Atleta4: TStringField;
    cdsTabellone8Atleta5: TStringField;
    cdsTabellone8Atleta6: TStringField;
    cdsTabellone8Atleta7: TStringField;
    cdsTabellone8Atleta8: TStringField;
    cdsTabellone4Atleta1: TStringField;
    cdsTabellone4Atleta2: TStringField;
    cdsTabellone4Atleta3: TStringField;
    cdsTabellone4Atleta4: TStringField;
    cdsAtletiApp: TClientDataSet;
    cdsAtletiAppCodice: TIntegerField;
    cdsAtletiAppNome: TStringField;
    cdsAtletiAppPeso: TFloatField;
    cdsAtletiAppCodicePalestra: TIntegerField;
    cdsAtletiAppCategoria: TStringField;
    cdsTabelloneIta4: TClientDataSet;
    cdsTabelloneIta4Atleta1: TStringField;
    cdsTabelloneIta4Atleta2: TStringField;
    cdsTabelloneIta4Atleta3: TStringField;
    cdsTabelloneIta4Atleta4: TStringField;
    cdsTabelloneIta3: TClientDataSet;
    cdsTabelloneIta5: TClientDataSet;
    cdsTabelloneIta3Atleta1: TStringField;
    cdsTabelloneIta3Atleta2: TStringField;
    cdsTabelloneIta3Atleta3: TStringField;
    cdsTabelloneIta5Atleta1: TStringField;
    cdsTabelloneIta5Atleta2: TStringField;
    cdsTabelloneIta5Atleta3: TStringField;
    cdsTabelloneIta5Atleta4: TStringField;
    cdsTabelloneIta5Atleta5: TStringField;
    cdsTabelloneIta3Palestra1: TStringField;
    cdsTabelloneIta3Palestra2: TStringField;
    cdsTabelloneIta3Palestra3: TStringField;
    cdsTabelloneIta4Palestra1: TStringField;
    cdsTabelloneIta4Palestra2: TStringField;
    cdsTabelloneIta4Palestra3: TStringField;
    cdsTabelloneIta4Palestra4: TStringField;
    cdsTabelloneIta5Palestra1: TStringField;
    cdsTabelloneIta5Palestra2: TStringField;
    cdsTabelloneIta5Palestra3: TStringField;
    cdsTabelloneIta5Palestra4: TStringField;
    cdsTabelloneIta5Palestra5: TStringField;
    cdsAtleti: TClientDataSet;
    cdsAtletiCodice: TIntegerField;
    cdsAtletiNome: TStringField;
    cdsAtletiPeso: TFloatField;
    cdsAtletiCodicePalestra: TIntegerField;
    cdsAtletiCategoria: TStringField;
    cdsAtletiPalestra: TStringField;
    cdsAtletiSesso: TStringField;
    cdsAtletiApp2: TClientDataSet;
    cdsAtletiApp2Nome: TStringField;
    cdsAtletiApp2CodicePalestra: TIntegerField;
    cdsAtletiApp2Peso: TFloatField;
    cdsAtletiApp2Categoria: TStringField;
    cdsAtletiApp1: TClientDataSet;
    cdsAtletiApp1Nome: TStringField;
    cdsAtletiApp1CodicePalestra: TIntegerField;
    cdsAtletiApp1Categoria: TStringField;
    cdsAtletiApp1Peso: TFloatField;
    cdsPalestre: TClientDataSet;
    cdsPalestreCodice: TIntegerField;
    cdsPalestreNome: TStringField;
    srcAtleti: TDataSource;
    srcPalestre: TDataSource;
    cdsAtletiGrado: TStringField;
    cdsAtletiAppGrado: TStringField;
    cdsAtletiApp2Grado: TStringField;
    cdsAtletiApp1Grado: TStringField;
    cdsAtletiAppSesso: TStringField;
    cdsAtletiApp1Sesso: TStringField;
    cdsAtletiApp2Sesso: TStringField;
    cdsTabellone2: TClientDataSet;
    cdsTabellone2Atleta1: TStringField;
    cdsTabellone2Atleta2: TStringField;
    cdsAtletiAnno: TIntegerField;
    cdsAtletiAppAnno: TIntegerField;
    cdsAtletiApp1Anno: TIntegerField;
    cdsAtletiApp2Anno: TIntegerField;
    procedure DataModuleCreate(Sender: TObject);
    procedure DataModuleDestroy(Sender: TObject);
    procedure cdsAtletiAfterOpen(DataSet: TDataSet);
    procedure cdsAtletiNewRecord(DataSet: TDataSet);
    procedure cdsPalestreAfterOpen(DataSet: TDataSet);
    procedure cdsPalestreNewRecord(DataSet: TDataSet);
    procedure cdsAtletiGradoGetText(Sender: TField; var Text: String;
      DisplayText: Boolean);
    procedure cdsAtletiGradoSetText(Sender: TField; const Text: String);
  private
    CodicePalestra, CodiceAtleta :Integer;
    CatBambiniList, CatUnder19MList, CatUnder19FList,
    CatRagazziMList, CatRagazziFList, CatEsAMList, CatEsBMList,
    CatEsAFList, CatEsBFList, CatCadettiMList,
    CatCadettiFList, CatJuniorMList, CatJuniorFList,
    CatSeniorMList, CatSeniorFList, CatMasterM1List, CatMasterF1List :TStringList;
    function GetNumeroAtleti: Integer;
  public
    property NumeroAtleti :Integer read GetNumeroAtleti;
    procedure TogliFiltro;
    procedure TogliOrdinamento;
    procedure Ordina;
    procedure MescolaPalestre;
    procedure MescolaAtleti;
    procedure SetFilterAtleti(AFilter :String);
    function GetCatPesoList(ACatPeso :TCatPeso) :String;
    procedure SetCatPesoList(ACatPeso :TCatPeso; AValue :String);
    procedure SaveAll;
    procedure AtletiMoveUp;
    procedure AtletiMoveDown;
    procedure RicalcolaCodici;
  end;

var
  modJudo: TmodJudo;

implementation

{$R *.dfm}

procedure TmodJudo.DataModuleCreate(Sender: TObject);
begin
  if FileExists('palestreDB.xml') then
    cdsPalestre.LoadFromFile('palestreDB.xml')
  else
    raise Exception.Create('Manca il file palestreDB.xml');
  if FileExists('judoDB.xml') then
    cdsAtleti.LoadFromFile('judoDB.xml')
  else
    raise Exception.Create('Manca il file judoDB.xml');

  cdsPalestre.First;
  cdsAtleti.First;
      
  cdsAtletiApp1.CreateDataSet;
  cdsAtletiApp2.CreateDataSet;

  CatBambiniList := TStringList.Create;
  CatUnder19MList := TStringList.Create;
  CatUnder19FList := TStringList.Create;

  CatRagazziMList := TStringList.Create;
  CatRagazziFList := TStringList.Create;
  CatEsAMList := TStringList.Create;
  CatEsBMList := TStringList.Create;
  CatEsAFList := TStringList.Create;
  CatEsBFList := TStringList.Create;
  CatCadettiMList := TStringList.Create;
  CatCadettiFList := TStringList.Create;
  CatJuniorMList := TStringList.Create;
  CatJuniorFList := TStringList.Create;
  CatSeniorMList := TStringList.Create;
  CatSeniorFList := TStringList.Create;
  CatMasterM1List := TStringList.Create;
  CatMasterF1List := TStringList.Create;

  if FileExists('bambini.txt') then
    CatBambiniList.LoadFromFile('bambini.txt');
  if FileExists('under19m.txt') then
    CatUnder19MList.LoadFromFile('under19m.txt');
  if FileExists('under19f.txt') then
    CatUnder19FList.LoadFromFile('under19f.txt');
  if FileExists('ragazziM.txt') then
    CatRagazziMList.LoadFromFile('ragazziM.txt');
  if FileExists('ragazziF.txt') then
    CatRagazziFList.LoadFromFile('ragazziF.txt');
  if FileExists('esordientiAM.txt') then
    CatEsAMList.LoadFromFile('esordientiAM.txt');
  if FileExists('esordientiBM.txt') then
    CatEsBMList.LoadFromFile('esordientiBM.txt');
  if FileExists('esordientiAF.txt') then
    CatEsAFList.LoadFromFile('esordientiAF.txt');
  if FileExists('esordientiBF.txt') then
    CatEsBFList.LoadFromFile('esordientiBF.txt');
  if FileExists('cadettiM.txt') then
    CatCadettiMList.LoadFromFile('cadettiM.txt');
  if FileExists('cadettiF.txt') then
    CatCadettiFList.LoadFromFile('cadettiF.txt');
 if FileExists('juniorM.txt') then
    CatJuniorMList.LoadFromFile('juniorM.txt');
  if FileExists('juniorF.txt') then
    CatJuniorFList.LoadFromFile('juniorF.txt');
  if FileExists('seniorM.txt') then
    CatSeniorMList.LoadFromFile('seniorM.txt');
  if FileExists('seniorF.txt') then
    CatSeniorFList.LoadFromFile('seniorF.txt');
  if FileExists('masterM1.txt') then
    CatMasterM1List.LoadFromFile('masterM1.txt');
  if FileExists('masterF1.txt') then
    CatMasterF1List.LoadFromFile('masterF1.txt');
end;

procedure TmodJudo.DataModuleDestroy(Sender: TObject);
begin
  SaveAll;

  CatBambiniList.Free;
  CatUnder19MList.Free;
  CatUnder19FList.Free;
  CatRagazziMList.Free;
  CatRagazziFList.Free;
  CatEsAMList.Free;
  CatEsBMList.Free;
  CatEsAFList.Free;
  CatEsBFList.Free;
  CatCadettiMList.Free;
  CatCadettiFList.Free;
  CatJuniorMList.Free;
  CatJuniorFList.Free;
  CatSeniorMList.Free;
  CatSeniorFList.Free;
  CatMasterM1List.Free;
  CatMasterF1List.Free;
end;

procedure TmodJudo.TogliFiltro;
begin
  cdsAtleti.Filtered := False;
end;

procedure TmodJudo.cdsAtletiAfterOpen(DataSet: TDataSet);
begin
  CodiceAtleta := 0;
  cdsAtleti.DisableControls;
  try
    cdsAtleti.First;
    while not cdsAtleti.Eof do
    begin
      if cdsAtletiCodice.AsInteger > CodiceAtleta then
        CodiceAtleta := cdsAtletiCodice.AsInteger;
      cdsAtleti.Next;
    end;
  finally
    cdsAtleti.EnableControls;
  end;
end;

procedure TmodJudo.cdsAtletiNewRecord(DataSet: TDataSet);
begin
  CodiceAtleta := CodiceAtleta + 1;
  cdsAtletiCodice.AsInteger := CodiceAtleta;
end;

procedure TmodJudo.Ordina;
var
  Palestra, Pos :Integer;
  Bookmark: TBookmark;
begin
  if cdsAtleti.IndexName <> '' then
    cdsAtleti.DeleteIndex(cdsAtleti.IndexName);
  if cdsPalestre.IndexName <> '' then
    cdsPalestre.DeleteIndex(cdsPalestre.IndexName);
  Pos := 1;
  cdsPalestre.DisableControls;
  try
    cdsPalestre.First;
    cdsAtleti.DisableControls;
    try
      while not cdsPalestre.Eof do
      begin
        Palestra := cdsPalestreCodice.AsInteger;
        cdsAtleti.First;
        while not cdsAtleti.Eof do
        begin
          if cdsAtletiCodicePalestra.AsInteger = Palestra then
          begin
            cdsAtletiApp1.Edit;
            cdsAtletiApp1Nome.AsString := cdsAtletiNome.AsString;
            cdsAtletiApp1CodicePalestra.AsInteger := cdsAtletiCodicePalestra.AsInteger;
            cdsAtletiApp1Peso.AsFloat := cdsAtletiPeso.AsFloat;
            cdsAtletiApp1Categoria.AsString := cdsAtletiCategoria.AsString;
            cdsAtletiApp1Grado.AsString := cdsAtletiGrado.AsString;
            cdsAtletiApp1Sesso.AsString := cdsAtletiSesso.AsString;
            cdsAtletiApp1Anno.AsInteger := cdsAtletiAnno.AsInteger;
            cdsAtletiApp1.Post;
            Bookmark := cdsAtleti.Bookmark;
            if cdsAtleti.RecordCount > Pos then
            begin
              cdsAtleti.RecNo := Pos;
              Inc(pos);
            end;
            cdsAtletiApp2.Edit;
            cdsAtletiApp2Nome.AsString := cdsAtletiNome.AsString;
            cdsAtletiApp2CodicePalestra.AsInteger := cdsAtletiCodicePalestra.AsInteger;
            cdsAtletiApp2Peso.AsFloat := cdsAtletiPeso.AsFloat;
            cdsAtletiApp2Categoria.AsString := cdsAtletiCategoria.AsString;
            cdsAtletiApp2Grado.AsString := cdsAtletiGrado.AsString;
            cdsAtletiApp2Sesso.AsString := cdsAtletiSesso.AsString;
            cdsAtletiApp2Anno.AsInteger := cdsAtletiAnno.AsInteger;
            cdsAtletiApp2.Post;

            cdsAtleti.Edit;
            cdsAtletiNome.AsString := cdsAtletiApp1Nome.AsString;
            cdsAtletiCodicePalestra.AsInteger := cdsAtletiApp1CodicePalestra.AsInteger;
            cdsAtletiPeso.AsFloat := cdsAtletiApp1Peso.AsFloat;
            cdsAtletiCategoria.AsString := cdsAtletiApp1Categoria.AsString;
            cdsAtletiGrado.AsString := cdsAtletiApp1Grado.AsString;
            cdsAtletiSesso.AsString := cdsAtletiApp1Sesso.AsString;
            cdsAtletiAnno.AsInteger := cdsAtletiApp1Anno.AsInteger;
            cdsAtleti.Post;

            cdsAtleti.Bookmark := TBytes(Bookmark);
            cdsAtleti.Edit;
            cdsAtletiNome.AsString := cdsAtletiApp2Nome.AsString;
            cdsAtletiCodicePalestra.AsInteger := cdsAtletiApp2CodicePalestra.AsInteger;
            cdsAtletiPeso.AsFloat := cdsAtletiApp2Peso.AsFloat;
            cdsAtletiCategoria.AsString := cdsAtletiApp2Categoria.AsString;
            cdsAtletiGrado.AsString := cdsAtletiApp2Grado.AsString;
            cdsAtletiSesso.AsString := cdsAtletiApp2Sesso.AsString;
            cdsAtletiAnno.AsInteger := cdsAtletiApp2Anno.AsInteger;
            cdsAtleti.Post;

          end;
          cdsAtleti.Next;
        end;
        cdsPalestre.Next;
      end;
    finally
      cdsAtleti.EnableControls;
    end;
  finally
    cdsPalestre.EnableControls;
  end;
  cdsAtleti.First;
end;

procedure TmodJudo.MescolaAtleti;
var
  Pos :Integer;
begin
  cdsAtletiApp.EmptyDataSet;
  cdsAtleti.DisableControls;
  try
    cdsAtleti.First;
    cdsAtleti.Edit;
    cdsAtletiApp.Edit;
    while not cdsAtleti.Eof do
    begin
      Pos := 0;
      Randomize;
      while (Pos <= 0) and (cdsAtleti.RecordCount <> 1) do
        Pos := Random(cdsAtleti.RecordCount);
      if Pos = 0 then
        Pos := 1;
      cdsAtleti.RecNo := Pos;
      cdsAtletiApp.Append;
      cdsAtletiAppCodice.AsInteger := cdsAtletiCodice.AsInteger;
      cdsAtletiAppNome.AsString := cdsAtletiNome.AsString;
      cdsAtletiAppCodicePalestra.AsString := cdsAtletiCodicePalestra.AsString;
      cdsAtletiAppPeso.AsFloat := cdsAtletiPeso.AsFloat;
      cdsAtletiAppCategoria.AsString := cdsAtletiCategoria.AsString;
      cdsAtletiAppGrado.AsString := cdsAtletiGrado.AsString;
      cdsAtletiAppSesso.AsString := cdsAtletiSesso.AsString;
      cdsAtletiAppAnno.AsInteger := cdsAtletiAnno.AsInteger;
      cdsAtletiApp.Post;
      cdsAtleti.Delete;
    end;
    cdsAtletiApp.First;
    while not modJudo.cdsAtletiApp.Eof do
    begin
      cdsAtleti.Append;
      cdsAtletiCodice.AsInteger := cdsAtletiAppCodice.AsInteger;
      cdsAtletiNome.AsString := cdsAtletiAppNome.AsString;
      cdsAtletiCodicePalestra.AsString := cdsAtletiAppCodicePalestra.AsString;
      cdsAtletiPeso.AsFloat := cdsAtletiAppPeso.AsFloat;
      cdsAtletiCategoria.AsString := cdsAtletiAppCategoria.AsString;
      cdsAtletiGrado.AsString := cdsAtletiAppGrado.AsString;
      cdsAtletiSesso.AsString := cdsAtletiAppSesso.AsString;
      cdsAtletiAnno.AsInteger := cdsAtletiAppAnno.AsInteger;
      cdsAtleti.Post;
      cdsAtleti.Next;
      cdsAtletiApp.Next;
    end;
  finally
    cdsAtleti.EnableControls;
  end;
  cdsAtleti.First;
end;

procedure TmodJudo.MescolaPalestre;
var
  Pos :Integer;
begin
  cdsPalestreApp.EmptyDataSet;
  cdsPalestre.DisableControls;
  try
    cdsPalestre.First;
    cdsPalestre.Edit;
    cdsPalestreApp.Edit;
    while not cdsPalestre.Eof do
    begin
      Pos := 0;
      Randomize;
      while (Pos <= 0) and (cdsPalestre.RecordCount <> 1) do
        Pos := Random(cdsPalestre.RecordCount);
      if Pos = 0 then
        Pos := 1;
      cdsPalestre.RecNo := Pos;
      cdsPalestreApp.Append;
      cdsPalestreAppCodice.AsInteger := cdsPalestreCodice.AsInteger;
      cdsPalestreAppNome.AsString := cdsPalestreNome.AsString;
      cdsPalestreApp.Post;
      cdsPalestre.Delete;
    end;
    cdsPalestreApp.First;
    while not cdsPalestreApp.Eof do
    begin
      cdsPalestre.Append;
      cdsPalestreCodice.AsInteger := cdsPalestreAppCodice.AsInteger;
      cdsPalestreNome.AsString := cdsPalestreAppNome.AsString;
      cdsPalestre.Post;
      cdsPalestre.Next;
      cdsPalestreApp.Next;
    end;
  finally
    cdsPalestre.EnableControls;
  end;
  cdsPalestre.First;
end;

procedure TmodJudo.cdsPalestreAfterOpen(DataSet: TDataSet);
begin
  CodicePalestra := 0;
  cdsPalestre.DisableControls;
  try
    cdsPalestre.First;
    while not cdsPalestre.Eof do
    begin
      if cdsPalestreCodice.AsInteger > CodicePalestra then
        CodicePalestra := cdsPalestreCodice.AsInteger;
      cdsPalestre.Next;
    end;
  finally
    cdsPalestre.EnableControls;
  end;
end;

procedure TmodJudo.cdsPalestreNewRecord(DataSet: TDataSet);
begin
  CodicePalestra := CodicePalestra + 1;
  cdsPalestreCodice.AsInteger := CodicePalestra;
end;

procedure TmodJudo.TogliOrdinamento;
begin
  if cdsAtleti.IndexName <> '' then
    cdsAtleti.DeleteIndex(cdsAtleti.IndexName);
  if cdsPalestre.IndexName <> '' then
    cdsPalestre.DeleteIndex(cdsPalestre.IndexName);
  cdsAtleti.First;
  cdsPalestre.First;
end;

procedure TmodJudo.SetFilterAtleti(AFilter: String);
begin
  cdsAtleti.Filter := AFilter;
  cdsAtleti.Filtered := True;
end;

function TmodJudo.GetCatPesoList(ACatPeso: TCatPeso): String;
begin
  Result := '';

  if ACatPeso = CatBambini then
    Result := CatBambiniList.Text
  else if ACatPeso = CatUnder19M then
    Result := CatUnder19MList.Text
  else if ACatPeso = CatUnder19F then
    Result := CatUnder19FList.Text
  else if ACatPeso = catRagazziM then
    Result := CatRagazziMList.Text
  else if ACatPeso = catRagazziF then
    Result := CatRagazziFList.Text
  else if ACatPeso = catEsAM then
    Result := CatEsAMList.Text
  else if ACatPeso = catEsAF then
    Result := CatEsAFList.Text
  else if ACatPeso = catEsBM then
    Result := CatEsBMList.Text
  else if ACatPeso = catEsBF then
    Result := CatEsBFList.Text
  else if ACatPeso = catCadettiM then
    Result := CatCadettiMList.Text
  else if ACatPeso = catCadettiF then
    Result := CatCadettiFList.Text
  else if ACatPeso = catJuniorM then
    Result := CatJuniorMList.Text
  else if ACatPeso = catJuniorF then
    Result := CatJuniorFList.Text
  else if ACatPeso = catSeniorM then
    Result := CatSeniorMList.Text
  else if ACatPeso = catSeniorF then
    Result := CatSeniorFList.Text
  else if ACatPeso = catMasterM1 then
    Result := CatMasterM1List.Text
  else if ACatPeso = catMasterF1 then
    Result := CatMasterF1List.Text;

end;

procedure TmodJudo.SetCatPesoList(ACatPeso: TCatPeso; AValue: String);
begin
  if ACatPeso = CatBambini then
    CatBambiniList.Text := AValue
  else if ACatPeso = CatUnder19M then
    CatUnder19MList.Text := AValue
  else if ACatPeso = CatUnder19F then
    CatUnder19FList.Text := AValue
  else if ACatPeso = catRagazziM then
    CatRagazziMList.Text := AValue
  else if ACatPeso = catRagazziF then
    CatRagazziFList.Text := AValue
  else if ACatPeso = catEsAM then
    CatEsAMList.Text := AValue
  else if ACatPeso = catEsAF then
    CatEsAFList.Text := AValue
  else if ACatPeso = catEsBM then
    CatEsBMList.Text := AValue
  else if ACatPeso = catEsBF then
    CatEsBFList.Text := AValue
  else if ACatPeso = catCadettiM then
    CatCadettiMList.Text := AValue
  else if ACatPeso = catCadettiF then
    CatCadettiFList.Text := AValue
  else if ACatPeso = catJuniorM then
    CatJuniorMList.Text := AValue
  else if ACatPeso = catJuniorF then
    CatJuniorFList.Text := AValue
  else if ACatPeso = catSeniorM then
    CatSeniorMList.Text := AValue
  else if ACatPeso = catSeniorF then
    CatSeniorFList.Text := AValue
  else if ACatPeso = catMasterM1 then
    CatMasterM1List.Text := AValue
  else if ACatPeso = catMasterF1 then
    CatMasterF1List.Text := AValue;
end;

procedure TmodJudo.cdsAtletiGradoGetText(Sender: TField; var Text: String;
  DisplayText: Boolean);
begin
  if Sender.AsString = 'B' then
    Text := 'BIANCA'
  else if Sender.AsString = 'G' then
    Text := 'GIALLA'
  else if Sender.AsString = 'A' then
    Text := 'ARANCIO'
  else if Sender.AsString = 'V' then
    Text := 'VERDE'
  else if Sender.AsString = 'U' then
    Text := 'BLU'
  else if Sender.AsString = 'M' then
    Text := 'MARRONE'
  else if Sender.AsString = 'N' then
    Text := 'NERA'
  else if Sender.AsString = 'D' then
    Text := 'DAN'
end;

procedure TmodJudo.cdsAtletiGradoSetText(Sender: TField;
  const Text: String);
begin
  if Text = 'BIANCA' then
    Sender.AsString := 'B'
  else if Text = 'GIALLA' then
    Sender.AsString := 'G'
  else if Text = 'ARANCIO' then
    Sender.AsString := 'A'
  else if Text = 'VERDE' then
    Sender.AsString := 'V'
  else if Text = 'BLU' then
    Sender.AsString := 'U'
  else if Text = 'MARRONE' then
    Sender.AsString := 'M'
  else if Text = 'NERA' then
    Sender.AsString := 'N'
  else if Text = 'DAN' then
    Sender.AsString := 'D'
end;

function TmodJudo.GetNumeroAtleti: Integer;
begin
  Result := cdsAtleti.RecordCount;
end;

procedure TmodJudo.SaveAll;
begin
  cdsAtleti.MergeChangeLog;
  cdsPalestre.MergeChangeLog;

  CatBambiniList.SaveToFile('bambini.txt');
  CatUnder19MList.SaveToFile('under19m.txt');
  CatUnder19FList.SaveToFile('under19f.txt');
  CatRagazziMList.SaveToFile('ragazziM.txt');
  CatRagazziFList.SaveToFile('ragazziF.txt');
  CatEsAMList.SaveToFile('esordientiAM.txt');
  CatEsBMList.SaveToFile('esordientiBM.txt');
  CatEsAFList.SaveToFile('esordientiAF.txt');
  CatEsBFList.SaveToFile('esordientiBF.txt');
  CatCadettiMList.SaveToFile('cadettiM.txt');
  CatCadettiFList.SaveToFile('cadettiF.txt');
  CatJuniorMList.SaveToFile('juniorM.txt');
  CatJuniorFList.SaveToFile('juniorF.txt');
  CatSeniorMList.SaveToFile('seniorM.txt');
  CatSeniorFList.SaveToFile('seniorF.txt');
  CatMasterM1List.SaveToFile('masterM1.txt');
  CatMasterF1List.SaveToFile('masterF1.txt');

  cdsAtleti.SaveToFile('judoDB.xml');
  cdsPalestre.SaveToFile('palestreDB.xml');
end;

procedure TmodJudo.AtletiMoveDown;
var
  ThisProg, NextProg :Integer;
begin
  if not cdsAtleti.IsEmpty then
  begin
    cdsAtleti.DisableControls;
    try
      ThisProg := cdsAtletiCodice.AsInteger;
      cdsAtleti.Next;
      NextProg := cdsAtletiCodice.AsInteger;
      if not cdsAtleti.Eof then
      begin
        cdsAtleti.Edit;
        cdsAtletiCodice.AsInteger := ThisProg;
        cdsAtleti.Post;
        cdsAtleti.Prior;
        cdsAtleti.Edit;
        cdsAtletiCodice.AsInteger := NextProg;
        cdsAtleti.Post;
      end;
    finally
      cdsAtleti.EnableControls;
    end;
  end;

end;

procedure TmodJudo.AtletiMoveUp;
var
  ThisProg, PriorProg: Integer;
  IndexName :String;
begin
  inherited;
  if not cdsAtleti.IsEmpty and (cdsAtleti.RecNo <> 1) then
  begin
    IndexName := cdsAtleti.IndexName;
    if cdsAtleti.IndexName <> '' then
      cdsAtleti.DeleteIndex(cdsAtleti.IndexName);

    cdsAtleti.DisableControls;
    try
      ThisProg := cdsAtletiCodice.AsInteger;
      cdsAtleti.Prior;
      PriorProg := cdsAtletiCodice.AsInteger;
      cdsAtleti.Edit;
      cdsAtletiCodice.AsInteger := ThisProg;
      cdsAtleti.Post;
      cdsAtleti.Prior;
      cdsAtleti.Edit;
      cdsAtletiCodice.AsInteger := PriorProg;
      cdsAtleti.Post;
    finally
      cdsAtleti.EnableControls;
    end;


(*    if IndexName <> '' then
      cdsAtleti.IndexName := IndexName;*)
  end;
end;

procedure TmodJudo.RicalcolaCodici;
var
  i :Integer;
begin
  TogliFiltro;
  TogliOrdinamento;
  i := 0;
  cdsAtleti.First;
  while not cdsAtleti.Eof do
  begin
    Inc(i);
    cdsAtleti.Edit;
    cdsAtletiCodice.AsInteger := i;
    cdsAtleti.Post;
    cdsAtleti.Next;
  end;
  cdsAtleti.First;
end;

end.

